<template>
	<view style="padding: 40rpx;">
		<view class="content_realization">
			手机号码
			<u-input border="none" class="realization_input" placeholder="请输入手机号" v-model="user.phone">
				<u--text text="+86 |" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
			</u-input>
			验证码
			<u-input border="none" class="realization_input" placeholder="请输入验证码" v-model="codeNum">
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒后重新获取"></u-code>
					<u-button @tap="getCode" :text="tips" type="info" size="mini"></u-button>
				</template>
			</u-input>
		</view>
		<button @click="tochangePassword" type="primary">下一步</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '获取验证码',
				codeNum: '',
				user: {},
				a: 0
			}
		},
		onLoad: function(e) {
			this.a = Number(e.a)
		},
		methods: {
			tochangePassword() {
				if (this.user.phone.length == 0) {
					uni.showToast({
						title: '请输入账号！',
						icon: 'none'
					})
				} else if (this.codeNum.length == 0) {
					uni.showToast({
						title: '请输入验证码！',
						icon: 'none'
					})
				} else {
					if (this.a == 0) {
						// 调用登录接口
						this.$api.iUserLogin(this.user).then(res => {
							console.log(res);
							if (res.code == 200) {
								// 登录成功
								uni.navigateTo({
									url: "./changePassword?phone=" + this.user.phone + "&a=" + this.a +
										"&id=" + res.data.id
								})
							} else {
								uni.$u.toast("用户名不存在")
							}
						})
					} else {
						// 调用登录接口
						this.$api.eUserLogin(this.user).then(res => {
							console.log(res);
							if (res.code == 200) {
								// 登录成功
								uni.navigateTo({
									url: "./changePassword?phone=" + this.user.phone + "&a=" + this.a +
										"&id=" + res.data.id
								})
							} else {
								uni.$u.toast("用户名不存在")
							}
						})
					}

				}
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		}
	}
</script>

<style>
	.realization_input {
		background-color: #f2f7fb;
		height: 80rpx;
	}

	.content_realization {
		line-height: 80rpx;
		margin-bottom: 40rpx;
	}
</style>
